package com.lago.homework3;

import java.util.Scanner;

public class Exercise2 {
    public static String getMaxSameString(String str1, String str2) {

        if (str1 == null || str1.length() == 0) {
            return null;
        }

        if (str2 == null || str2.length() == 0) {
            return null;
        }

        for (int i = str2.length(); i >= 1; i--) {
            for (int j = 0; (j + i) <= str2.length(); j++) {
                String substring = str2.substring(j, j + i);
                if (str1.contains(substring)) {
                    return substring;
                }
            }
        }
        return null;
    }


    public static void main(String[] args) {
        System.out.println("请输入第一个字符串：");
        Scanner sc1 = new Scanner(System.in);
        String str1 = sc1.next();
        System.out.println("请输入第二个字符串：");
        Scanner sc2 = new Scanner(System.in);
        String str2 = sc2.next();

        System.out.println("-------------------------");
        System.out.println("获取到的最长相同字符串为：");
        System.out.println(getMaxSameString(str1, str2));
    }
}
